//
//  ViewController.m
//  第二题
//
//  Created by dllo on 16/6/6.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"
#import "CreateImageView.h"
#import "ZombieImageView.h"
#import "Myslider.h"

/*
 使 UIImageView实现 <植物大战僵尸>:
 需求:
 1. 三种植物实现动态图
 2. 三个僵尸实现走动的动态图 
 3. (不要求有移动的效果)
 */

@interface ViewController ()

@end

@implementation ViewController

-(BOOL)prefersStatusBarHidden
{
    return YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self creatImage];
    [self createSlider];
    
}

#pragma mark - 创建slider
-(void)createSlider
{
    Myslider *slider = [[Myslider alloc]initWithFrame:CGRectMake(100, 650, 150, 50)];
    [self.view addSubview:slider];
    [slider release];
    [slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
}

#pragma mark - 添加方法
-(void)sliderAction:(Myslider *)slider
{
    /**第一个植物*/
    CreateImageView *animationImageViewOfPlants = [CreateImageView initWithFrame:CGRectMake(50, 120, 100, 100) withName:@"plants1.tiff"];
    [self.view addSubview:animationImageViewOfPlants];
    [animationImageViewOfPlants release];
    [self createAnimationWithName:@"plants" withNum:10 withView:animationImageViewOfPlants withSlider:slider];
    
    /**第二个植物*/
    CreateImageView *animationImageViewOfGua = [CreateImageView initWithFrame:CGRectMake(50, 320, 100, 100) withName:@"gua1.tiff"];
    [self.view addSubview:animationImageViewOfGua];
    [animationImageViewOfGua release];
    [self createAnimationWithName:@"gua" withNum:17 withView:animationImageViewOfGua withSlider:slider];
    
    /**第三个植物*/
    CreateImageView *animationImageViewOfFlower = [CreateImageView initWithFrame:CGRectMake(50, 520, 100, 100) withName:@"flower1.tiff"];
    [self.view addSubview:animationImageViewOfFlower];
    [animationImageViewOfFlower release];
    [self createAnimationWithName:@"flower" withNum:19 withView:animationImageViewOfFlower withSlider:slider];
    
    /**创建僵尸*/
    for (int i = 0; i < 3; i++) {
        ZombieImageView *animationImageViewOfZombie = [[ZombieImageView alloc]initWithFrame:CGRectMake(230, 30 + 200 * i, 200, 200)];
        [self.view addSubview:animationImageViewOfZombie];
        [animationImageViewOfZombie release];
        [self createAnimationWithName:@"BZombie" withNum:26 withView:animationImageViewOfZombie withSlider:slider];
    }
}

#pragma mark - 创建背景图片
-(void)creatImage
{
    UIImage *image = [UIImage imageNamed:@"BackGround.png"];
    UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
    imageView.frame = [UIScreen mainScreen].bounds;
    [self.view addSubview:imageView];
    [imageView release];
}

#pragma mark - 动图
-(void)createAnimationWithName:(NSString *)name withNum:(NSInteger)num withView:(UIImageView *)imageView withSlider:(Myslider *)slider{
    
    NSMutableArray *array = [NSMutableArray array];
    for (int i = 1; i < num; i++) {
        NSString *newName = [NSString stringWithFormat:@"%@%d.tiff", name, i];
        UIImage *image = [UIImage imageNamed:newName];
        [array addObject:image];
    }
    imageView.animationImages = array;
    imageView.animationDuration = slider.value / 100;
    [imageView startAnimating];
}


@end
















